package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;


import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

public class TianYaBlogAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://blog.tianya.cn/blogger/.*[.]asp[?]BlogID=[0-9]+&PostID=[0-9]+",
				"http://blog.tianya.cn/blogger/.*[.]asp[?]PostID=[0-9]+&BlogID=[0-9]+",
				"http://blog.tianya.cn/post-[0-9]+-[0-9]+-1.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet = true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if(!isDetailPage(url)){
		}
		String htmltxt = urlMeta.getHtml();
		Document doc = Jsoup.parse(htmltxt);
		NewsMeta blog = new NewsMeta();
		String title = "";
		String content = "";
		Long date = null;
		Integer commentNum = 0;
		Integer clickNum = 0;
		Integer deliverNum = 0;
		String author = null;
		/**
		 * 标题
		 */
		title = doc.select("h2").text();
		if(StringUtils.isBlank(title)){
			title = doc.select("div.vtitle").text();
			if(StringUtils.isBlank(title)){
				title = doc.select("div.title1").text();
			}
		}
		author = doc.select("a.userName").text();
		/**
		 * 时间
		 */
		date = DateUtils.matchDate(doc.select("span.pos-right.gray6").text());
		if(date == null){
			date = DateUtils.matchDate(doc.select("div.vtime").text());
			if(date == null){
				date = DateUtils.matchDate(doc.select("p.view_time").text());
				if(date == null){
					date = DateUtils.matchDate(doc.select("span.time").text());
					if(date == null){
						date = DateUtils.matchDate(doc.select("div.posttop").text());
					}
				}
			}
		}
		/**
		 * 内容
		 */
		content = HtmlCleaner.getContentHtml(url,  doc.select("div.article-summary.articletext"));
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url,  doc.select("div.vcon"));
			if(StringUtils.isBlank(content)){
				content = HtmlCleaner.getContentHtml(url,  doc.select("p.view_con"));
				if(StringUtils.isBlank(content)){
					content = HtmlCleaner.getContentHtml(url,  doc.select("div.content"));
				}
			}
		}
		/**
		 * 查看，评论
		 */
		Elements select = doc.select("div.article-categories.pos-relative");
		if(select.size() > 0){
			String matchDate = select.toString();
			Pattern clickReg = Pattern.compile("浏览:[0-9]*");
			Matcher matcher_click = clickReg.matcher(matchDate);
			if (matcher_click.find()) {
				 String click = matcher_click.group();
				 if(click != null){
					 click = click.replace("浏览:", "");
					 try {
						clickNum = Integer.parseInt(click);
					} catch (NumberFormatException e) {
						clickNum = 0;
					}
				 }
			}
			Pattern commentReg = Pattern.compile("评论:[0-9]*");
			Matcher matcher_comment = commentReg.matcher(matchDate);
			if (matcher_comment.find()) {
				 String comment = matcher_comment.group();
				 if(comment != null){
					 comment = comment.replace("评论:", "");
					 try {
						commentNum = Integer.parseInt(comment);
					} catch (NumberFormatException e) {
						commentNum = 0;
					}
				 }
			}
			blog.setUpdateUrl(url);
		}else{
			select = doc.select("div.vtime");
			if(select.size() > 0){
				String matchDate = select.toString();
				Pattern clickReg = Pattern.compile("访问量：[0-9]*");
				Matcher matcher_click = clickReg.matcher(matchDate);
				if (matcher_click.find()) {
					 String click = matcher_click.group();
					 if(click != null){
						 click = click.replace("访问量：", "");
						 try {
							clickNum = Integer.parseInt(click);
						} catch (NumberFormatException e) {
							clickNum = 0;
						}
					 }
					 blog.setUpdateUrl(url);
				}
			}else{
				select = doc.select("p.view_time");
				if(select.size() > 0){
					String matchDate = select.toString();
					Pattern clickReg = Pattern.compile("访问量：[0-9]*");
					Matcher matcher_click = clickReg.matcher(matchDate);
					if (matcher_click.find()) {
						 String click = matcher_click.group();
						 if(click != null){
							 click = click.replace("访问量：", "");
							 try {
								clickNum = Integer.parseInt(click);
							} catch (NumberFormatException e) {
								clickNum = 0;
							}
						 }
						 blog.setUpdateUrl(url);
					}
				}else{
					select = doc.select("span.browsing-times");
					if(select.size() > 0){
						String matchDate = select.toString();
						Pattern clickReg = Pattern.compile("浏览：[0-9]*");
						Matcher matcher_click = clickReg.matcher(matchDate);
						if (matcher_click.find()) {
							 String click = matcher_click.group();
							 if(click != null){
								 click = click.replace("浏览：", "");
								 try {
									clickNum = Integer.parseInt(click);
								} catch (NumberFormatException e) {
									clickNum = 0;
								}
							 }
							 blog.setUpdateUrl(url);
						}
					}
				}
			}
		}
		if(StringUtils.isBlank(title)){
		}
		blog.setUrl(url);
		blog.setTitle(title);
		blog.setContent(content);
		blog.setDate(date);
		blog.setAuthor(author);
		blog.setClickNum(clickNum);
		blog.setCommentNum(commentNum);
		//blog.setDeliverNum(deliverNum);
		blog.setType(7);
		return blog;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta != null){
			String url = meta.getUpdateUrl();
			if(!StringUtils.isBlank(url)){
				UrlMeta urlMeta = CrawlHTML.responseToURL(url);
				String htmltxt = urlMeta.getHtml();
				Document doc = Jsoup.parse(htmltxt);
				/**
				 * 查看，评论
				 */
				Integer commentNum = 0;
				Integer clickNum = 0;
				Elements select = doc.select("div.article-categories.pos-relative");
				if(select.size() > 0){
					String matchDate = select.toString();
					Pattern clickReg = Pattern.compile("浏览:[0-9]*");
					Matcher matcher_click = clickReg.matcher(matchDate);
					if (matcher_click.find()) {
						 String click = matcher_click.group();
						 if(click != null){
							 click = click.replace("浏览:", "");
							 try {
								clickNum = Integer.parseInt(click);
							} catch (NumberFormatException e) {
								clickNum = meta.getClickNum();
							}
						 }
					}
					Pattern commentReg = Pattern.compile("评论:[0-9]*");
					Matcher matcher_comment = commentReg.matcher(matchDate);
					if (matcher_comment.find()) {
						 String comment = matcher_comment.group();
						 if(comment != null){
							 comment = comment.replace("评论:", "");
							 try {
								commentNum = Integer.parseInt(comment);
							} catch (NumberFormatException e) {
								commentNum = meta.getCommentNum();
							}
						 }
					}
					meta.setClickNum(clickNum);
					meta.setCommentNum(commentNum);
					return meta;
				}else{
					select = doc.select("div.vtime");
					if(select.size() > 0){
						String matchDate = select.toString();
						Pattern clickReg = Pattern.compile("访问量：[0-9]*");
						Matcher matcher_click = clickReg.matcher(matchDate);
						if (matcher_click.find()) {
							 String click = matcher_click.group();
							 if(click != null){
								 click = click.replace("访问量：", "");
								 try {
									clickNum = Integer.parseInt(click);
								} catch (NumberFormatException e) {
									clickNum = meta.getClickNum();
								}
							 }
							 meta.setClickNum(clickNum);
							 return meta;
						}
					}else{
						select = doc.select("p.view_time");
						if(select.size() > 0){
							String matchDate = select.toString();
							Pattern clickReg = Pattern.compile("访问量：[0-9]*");
							Matcher matcher_click = clickReg.matcher(matchDate);
							if (matcher_click.find()) {
								 String click = matcher_click.group();
								 if(click != null){
									 click = click.replace("访问量：", "");
									 try {
										clickNum = Integer.parseInt(click);
									} catch (NumberFormatException e) {
										clickNum = meta.getClickNum();
									}
								 }
								 meta.setClickNum(clickNum);
								 return meta;
							}
						}else{
							select = doc.select("span.browsing-times");
							if(select.size() > 0){
								String matchDate = select.toString();
								Pattern clickReg = Pattern.compile("浏览：[0-9]*");
								Matcher matcher_click = clickReg.matcher(matchDate);
								if (matcher_click.find()) {
									 String click = matcher_click.group();
									 if(click != null){
										 click = click.replace("浏览：", "");
										 try {
											clickNum = Integer.parseInt(click);
										} catch (NumberFormatException e) {
											clickNum = meta.getClickNum();
										}
									 }
									 meta.setClickNum(clickNum);
									 return meta;
								}
							}
						}
					}
				}
			}
		}
		return null;
	}

	/**
	 * @param args
	 */
	

	
}
